//
//  OPShortcutConfigCell.m
//  LCGOptimusPrime
//
//  Created by 罗腾 on 2017/7/30.
//  Copyright © 2017年 erics. All rights reserved.
//

#import "OPShortcutConfigCell.h"

@interface OPShortcutConfigCell()
{
    UIImageView *iconImageView;
    UILabel *titleLab;
    CAShapeLayer *_borderLayer;
}
@end

@implementation OPShortcutConfigCell


-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self buidUI];
    }return self;
}


-(void)buidUI{
    
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;
    
    
    iconImageView = [[UIImageView alloc] init];
    [self addSubview:iconImageView];
    
    
    titleLab = [[UILabel alloc] init];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor darkTextColor];
    titleLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:titleLab];
    
    UIImageView * statusImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-19, 5, 14, 14)];
    statusImageView.backgroundColor = [UIColor blueColor];
    self.statusImageView = statusImageView;
    [self addSubview:statusImageView];
    
    UITapGestureRecognizer * tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(statusImageViewTapRecognizerAction)];
    [statusImageView addGestureRecognizer:tap];
    statusImageView.userInteractionEnabled = YES;
    
    
    [self addBorderLayer];
    
    
}

#pragma mark -  Event Response
- (void)statusImageViewTapRecognizerAction
{
    if ([self.handleDelegate respondsToSelector:@selector(cellHandleWithStatuImageViewMode:withIndexPath:)]) {
        
        UICollectionView * superView = [self collectionView];
        
        NSIndexPath * indexPath = [superView indexPathForCell:self];
        
        [self.handleDelegate cellHandleWithStatuImageViewMode:self.StatuImageViewMode withIndexPath:indexPath];
    }
}


//获取cell所在collectionView
- (UICollectionView *)collectionView
{
    UIResponder *next = self.nextResponder;
    while (next != nil) {
        if ([next isKindOfClass:[UICollectionView class]]) {
            return (UICollectionView *)next;
        }
        next = next.nextResponder;
    }
    return nil;
}


#pragma mark -  Private Methods
-(void)addBorderLayer{
    _borderLayer = [CAShapeLayer layer];
    _borderLayer.bounds = self.bounds;
    _borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:_borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    _borderLayer.lineWidth = 1.5f;
    _borderLayer.lineDashPattern = @[@5, @3];
    _borderLayer.fillColor = [UIColor clearColor].CGColor;
    _borderLayer.strokeColor = [UIColor colorWithRed:204/255.0f green:204/255.0f blue:204/255.0f alpha:1].CGColor;
    [self.layer addSublayer:_borderLayer];
    _borderLayer.hidden = true;
}



#pragma mark -  Getters and Setters
-(void)setTitle:(NSString *)title{
    _title = title;
    titleLab.text = title;
}
-(void)setIconName:(NSString *)iconName{
    _iconName = iconName;
    
    UIImage *image = [UIImage imageNamed:_iconName];
    iconImageView.image = [UIImage imageNamed:_iconName];
    
    CGSize size = self.bounds.size;
    iconImageView.frame = CGRectMake(0, 0, image.size.width,image.size.height);
    iconImageView.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/3);
    titleLab.frame = CGRectMake(0, self.bounds.size.height * 0.6, size.width, size.height-image.size.height);
    
}



-(void)setIsMoving:(BOOL)isMoving{
    _isMoving = isMoving;
    if (_isMoving) {
        titleLab.textColor = [UIColor lightGrayColor];
        iconImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Gray",_iconName]];
        _borderLayer.hidden = true;
    }else{
        titleLab.textColor = [UIColor darkTextColor];
        iconImageView.image = [UIImage imageNamed:_iconName];
        _borderLayer.hidden = true;
    }
    
}
@end
